<?php


namespace broadcast\services;

use app\models\system\SystemWebsite;
use broadcast\redis\setRedis;
use app\Request;
//reids 记录
class WechatService
{
    //url链接
    private $urlLst = [
        "sign" => 'https://open.weixin.qq.com/connect/qrconnect',
        'asston' => 'https://api.weixin.qq.com/sns/oauth2/access_token',
        'login' => 'https://api.weixin.qq.com/sns/userinfo',
        'moblie' => 'https://open.weixin.qq.com/connect/oauth2/authorize'
    ];

    //获取微信开放平台
    private $websiteLst = [
        'app_id'   => 'wx4343eec5cb60bb8a',
        'secret'   => '325256935e2b205c57efd2a08de3cc5a',
        'redirect' => 'https://vipwm.cn/WechatService.php?act=redirect'
    ];

    //获取微信公众平台
    private $moblieLst = [
        'app_id'  => 'wx6784344e021b6051',
        'secret'  => 'dbd6e95ffd162b515b1cbfc0f854cb24',
        'redirect'=> 'https://vipwm.cn/WechatService.php?act=redirect1'
    ];

    public function __construct ($website) 
    {
        // 处理配置
        //获取网站信息  start
        $website = setRedis::getCache(session("website"),'info');
        if (empty($website)) {
            $website = SystemWebsite::where("website_ident",session("website"))->find();
            if ($website) $website = $website->toArray();
            setRedis::setCache(session("website"),"info",$website,0);
        }
        //获取网站信息  end

        // 设置配置信息
        $primaryLink = str_replace('/index','',$website['reception_domain']);
        $primaryLink = 'https://measureback.wm63.com';
        $this->websiteLst['redirect'] = $primaryLink . "/wechat/login"; 
        // $this->moblieLst['redirect'] = $primaryLink . "/redirectp"; 


        //设置微信开放平台
        $this->websiteLst['app_id'] = isset($website['open_platform_appid']) ? $website['open_platform_appid'] : '';
        $this->websiteLst['secret'] = isset($website['open_platform_secrt']) ? $website['open_platform_secrt'] : '';


        //设置微信公众平台
        $this->moblieLst['app_id'] = isset($website['public_platform_appid']) ? $website['public_platform_appid'] : '';
        $this->moblieLst['secret'] = isset($website['public_platform_secrt']) ? $website['public_platform_secrt'] : '';

    }

    //pc端授权
    public function websiteAuth ()
    {
        $websiteLst =  $this->websiteLst;
        $urlLst     =  $this->urlLst;
        $url = $urlLst['sign']."?appid={$websiteLst['app_id']}&redirect_uri=".urlEncode($websiteLst['redirect'])."&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
        return $url;
    }

    //移动端授权
    public function moblieAuth ()
    {
        $moblieLst =  $this->moblieLst;
        $urlLst    =  $this->urlLst;
        $url = $urlLst['moblie']."?appid={$moblieLst['app_id']}&redirect_uri=".urlEncode($moblieLst['redirect'])."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        return $url;
    }
    
    //处理用户信息
    public function handleUSe ($code) {
        $websiteLst =  $this->websiteLst;
        $urlLst     =  $this->urlLst;
        //获取token&用户的openid
        $url = $urlLst['asston']."?appid={$websiteLst['app_id']}&secret={$websiteLst['secret']}&grant_type=authorization_code&code={$code}";
        $json = $this->httpGet($url);
        $json = json_decode($json,true);


        $url = $urlLst['login']."?access_token={$json['access_token']}&openid={$json['openid']}";
        $json = $this->httpGet($url);
        $json = json_decode($json,true);


        $data = array();
        $data['unionid'] = $json['unionid'];
        $data['username'] = $json['nickname'];
        $data['openid'] = $json['openid'];
        $data['headimgurl'] = $json['headimgurl'];
        $data['sex'] = $json['sex'];
        $data['password'] = md5(123456);
        return $data;
    }
    
    public function httpGet($url) {
        $curl = curl_init();    //初始化一个cURL会话。
        curl_setopt($curl, CURLOPT_TIMEOUT, 100);  //设置cURL允许执行的最长秒数
        curl_setopt($curl, CURLOPT_URL, $url);  //URL地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);  //禁用后cURL将终止从服务端进行验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);  //不验证证书是否存在
        curl_setopt($curl, CURLOPT_HEADER, FALSE);    //禁止后使用CURL_TIMECOND_IFUNMODSINCE，默认值为CURL_TIMECOND_IFUNMODSINCE
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出
        $res = curl_exec($curl);  //执行一个cURL会话
        curl_close($curl);  //关闭一个cURL会话
        return $res;
    }



}
